/*
 * =====================================================================================
 *
 *       Filename:  logger.h
 *
 *    Description:  打印日志，保证线程安全
 *
 *        Version:  1.0
 *        Created:  2008年08月27日 14时50分21秒
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  ZhixiongHong (hzx), fishwarter@gmail.com
 *        Company:  Unioncast
 *
 * =====================================================================================
 */

#ifndef SS_LOGGER_H
#define SS_LOGGER_H

#include <string>
#include <fstream>
using namespace std;

#ifdef HAS_SS_NAMESPACE
namespace ss {
#endif


class Logger
{
public:
	enum LEVEL
	{
		LOG_CRITICAL		=	50,
		LOG_ERROR			= 	40,
		LOG_WARNING			= 	30,
		LOG_INFO			= 	20,
		LOG_DEBUG			=	10,
		LOG_NOTSET			=	0
	};

	enum 
	{
		MAX_LOG_LEN		=	4096
	};
public:
	Logger():_level(LOG_DEBUG), _logfile(""), _isOpen(false){}
	Logger(int level, string logfile);
public:
	bool open_log_file(string filename, int level=LOG_DEBUG);	

	int	log(int level, string& content);
	int log(int level, const char* fmt, ...);

	int debug(string& content);
	int debug(const char* fmt, ...);

	int info(string& content);
	int info(const char* fmt, ...);

	int error(string& content);
	int error(const char* fmt, ...);

	int warning(string& content);
	int warning(const char* fmt, ...);

	int critical(string& content);
	int critical(const char* fmt, ...);

	void setlevel(int level)
	{
		_level = level;
	}
private:
	string get_level_info(int level);
	int writelog(int level, const char* content);
private:
	int			_level;
	string 		_logfile;
	bool		_isOpen;
	ofstream	_logfs;
	//string		_threadName;
	
};

extern Logger* g_logger;

#ifdef HAS_SS_NAMESPACE
}
#endif
#endif // SS_LOGGER_H
